package com.whut.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.*;

public class EquipCheck {
    public static List<Integer> countRiskPoint(List<Map<String , Object>> list){
        int riskPoint1 = 0;
        int riskPoint2 = 0;
        int riskPoint3 = 0;
        int riskPoint4 = 0;
        Map<String, String> map = new HashMap<String,String>();
        map.put("四级隐患","1");
        map.put("三级隐患","2");
        map.put("二级隐患","3");
        map.put("一级隐患","4");
        String test="1";
        int m =Integer.parseInt(test);
        for (int i = 0 ;i< list.size() ; i++){
            if(list.get(i).get("riskPoint").toString().equals("四级隐患")||list.get(i).get("riskPoint").toString().equals("一级隐患")||
            list.get(i).get("riskPoint").toString().equals("二级隐患")||
            list.get(i).get("riskPoint").toString().equals("三级隐患")){
            switch (Integer.parseInt(map.get(list.get(i).get("riskPoint").toString()))){
                case 1:
                    riskPoint1++;
                    break;
                case 2:
                    riskPoint2++;
                    break;
                case 3:
                    riskPoint3++;
                    break;
                case 4:
                    riskPoint4++;
                    break;
                    default:
            }}
            else{
                switch (Integer.parseInt(map.get(list.get(i).get("riskPoint").toString()))){
                    case 1:
                        riskPoint1++;
                        break;
                    case 2:
                        riskPoint2++;
                        break;
                    case 3:
                        riskPoint3++;
                        break;
                    case 4:
                        riskPoint4++;
                        break;
                    default:
                }
            }
        }
    //Map<String,Object> riskPointList = new HashMap<>();
        List<Integer> riskPointList = new ArrayList<>();
        riskPointList.add(list.size());
        riskPointList.add(riskPoint1);
        riskPointList.add(riskPoint2);
        riskPointList.add(riskPoint3);
        riskPointList.add(riskPoint4);

        return riskPointList;
    }
    //DivideCheckpartAndDes
    public static List<Map<String, Object>> divideCheckpartAndDes(List<Map<String, Object>> list){
        List<Map<String, Object>> problemList = list;
        List<Map<String, Object>> newProblemList = new ArrayList<>();
       // String[] allDividedCP;
        //List<String> allDividedCP= new ArrayList<>();
        for (int u = 0; u < problemList.size(); u++) {
            List<String> allDividedCP= new ArrayList<>();
            String riskPoint = problemList.get(u).get("riskPoint").toString();
            String cp = problemList.get(u).get("checkPart").toString();
            String[] tempCP = cp.split(",");
            String[] tempdividedCP=cp.split(",");
            for (int i = 0 ;i< tempCP.length ; i++){
                tempdividedCP = tempCP[i].split(";");
                for (int j = 0 ; j < tempdividedCP.length ; j ++){
                allDividedCP.add(tempdividedCP[j]);}
            }
//            String[] dividedCP = cp.split(",");
//            if (dividedCP.length ==1){
//                dividedCP = dividedCP[0].split(";");
//            }
            String des = problemList.get(u).get("problemDescription").toString();
            String[] dividedDes = des.split("；");
            String[] singleEquipName = allDividedCP.get(allDividedCP.size() - 1).split("#");
            //拼接数据
                for (int j = 0; j < allDividedCP.size(); j++) {
                    HashMap<String, Object> map = new HashMap<String, Object>();
                    map.put("checkPart", allDividedCP.get(j));
                    try {
                        map.put("problemDescription", dividedDes[j]);
                    } catch (Exception e) { map.put("problemDescription", dividedDes[0]);};
/*                    if (dividedDes.length > 1) map.put("problemDescription", dividedDes[j]);
                        //同样错误时不用分开
                    else map.put("problemDescription", dividedDes[0]);*/
                    String riskPoint1 = "";
                    switch (riskPoint) {
                        case "提醒":
                            riskPoint1 = "四级隐患";
                            break;
                        case "1":
                            riskPoint1 = "四级隐患";
                            break;
                        case "2":
                            riskPoint1 = "三级隐患";
                            break;
                        case "3":
                            riskPoint1 = "二级隐患";
                            break;
                        case "4":
                            riskPoint1 = "一级隐患";
                            break;
                        default:
                            riskPoint1 = "";
                            break;
                    }
                    map.put("riskPoint", riskPoint1);
                    newProblemList.add(map);
                }
  /*              String[] singleEquipName = dividedCP[dividedCP.length - 1].split("#");
            //拼接数据
                for (int j = 0; j < dividedCP.length; j++) {156156
                    HashMap<String, Object> map = new HashMap<String, Object>();
                    map.put("checkPart", dividedCP[j]);
                    if (dividedDes.length > 1) map.put("problemDescription", dividedDes[j]);
                        //同样错误时不用分开
                    else map.put("problemDescription", dividedDes[0]);
                    map.put("riskPoint", riskPoint);
                    newProblemList.add(map);
                }*/

        }
        return newProblemList;
    }

    //按照riskPoint
    public static void sortByRiskPointDesc(List<Map<String, Object>> list) {
        list.sort(new RiskPointComparator());
    }

    public static void sortByCheckPartDesc(List<Map<String, Object>> list){
        list.sort(new CheckPartComparator());
    }
    static class CheckPartComparator implements Comparator {
        @Override
        public int compare(Object a, Object b) {
            String checkParta = (String) JSONObject.parseObject(JSON.toJSONString(a)).get("checkPart");
            String checkPartb = (String) JSONObject.parseObject(JSON.toJSONString(b)).get("checkPart");
            String[] checkPartas = checkParta.split("#");
            checkParta = checkPartas[0];
            String[] checkPartbs = checkPartb.split("#");
            checkPartb = checkPartbs[0];
            int flag = 0;
            try {
                Integer temp1 = Integer.parseInt(checkPartas[0]);
                Integer temp2 = Integer.parseInt(checkPartbs[0]);
                if (temp1.compareTo(temp2) > 0) {
                    flag = 1;
                } else if (temp1.compareTo(temp2) == 0) {
                    flag = 0;
                } else {
                    flag = -1;
                }
            } catch (Exception e) {
                if (checkParta.compareTo(checkPartb) > 0) {
                    flag = 1;
                } else if (checkParta.compareTo(checkPartb) == 0) {
                    flag = 0;
                } else {
                    flag = -1;
                }
            }
            return flag;
        }
    }
    //重写比较riskPoint
    static class RiskPointComparator implements Comparator {
        @Override
        public int compare(Object o1, Object o2) {
            Map map1 = JSONObject.parseObject(JSONObject.toJSONString(o1), Map.class);
            Map map2 = JSONObject.parseObject(JSONObject.toJSONString(o2), Map.class);
            Map<String, String> map = new HashMap<String,String>();
            map.put("四级隐患","1");
            map.put("三级隐患","2");
            map.put("二级隐患","3");
            map.put("一级隐患","4");
            int num1;
            int num2;
            if(map1.get("riskPoint").equals("四级隐患")||map1.get("riskPoint").equals("三级隐患")||
                    map1.get("riskPoint").equals("二级隐患")||
                    map1.get("riskPoint").equals("一级隐患")){
                 num1 = Integer.parseInt((String) map.get(map1.get("riskPoint")));
                 num2 = Integer.parseInt((String) map.get(map2.get("riskPoint")));
            }
            else{
                 num1 = Integer.parseInt((String) map1.get("riskPoint"));
                 num2 = Integer.parseInt((String) map2.get("riskPoint"));
            }

            if (num1 == num2) {
                return 0;
            } else if (num1 < num2) {
                return 1;
            }
            return -1;
        }
    }
    public static <T> List<T> castList(Object obj, Class<T> clazz)
    {
        List<T> result = new ArrayList<T>();
        if(obj instanceof List<?>)
        {
            for (Object o : (List<?>) obj)
            {
                result.add(clazz.cast(o));
            }
            return result;
        }
        return null;
    }


    //比较checkPart首字母
    public static void sortCheckPart(List<Map<String, Object>> list) {
        Collections.sort(list, new ComparatCheckPart());
    }

    //重写比较checkPart首字母
    static class ComparatCheckPart implements Comparator {

        @Override
        public int compare(Object o1, Object o2) {
            Map map1 = JSONObject.parseObject(JSONObject.toJSONString(o1), Map.class);
            Map map2 = JSONObject.parseObject(JSONObject.toJSONString(o2), Map.class);
            String s1 = (String) map1.get("checkPart");
            String[] apart1 = s1.split("#");
            String s2 = (String) map2.get("checkPart");
            String[] apart2 = s2.split("#");
            int num1 = Integer.parseInt(apart1[0]);
            int num2 = Integer.parseInt(apart2[0]);
            if (num1 == num2) {
                return 0;
            } else if (num1 > num2) {
                return 1;
            }
            return -1;
        }
    }
}
